package cn.gcyl.module.biz.controller.admin.meetingplan;

import cn.gcyl.framework.apilog.core.annotation.ApiAccessLog;
import cn.gcyl.framework.common.pojo.CommonResult;
import cn.gcyl.framework.common.pojo.PageParam;
import cn.gcyl.framework.common.pojo.PageResult;
import cn.gcyl.framework.common.util.object.BeanUtils;
import cn.gcyl.framework.excel.core.util.ExcelUtils;
import cn.gcyl.module.biz.controller.admin.meetingplan.vo.*;
import cn.gcyl.module.biz.dal.dataobject.meetingplan.MeetingPlanDO;
import cn.gcyl.module.biz.service.meetingplan.MeetingPlanService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.gcyl.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.gcyl.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 参会方案")
@RestController
@RequestMapping("/biz/meeting-plan")
@Validated
public class MeetingPlanController {

    @Resource
    private MeetingPlanService meetingPlanService;

    @PostMapping("/create")
    @Operation(summary = "创建参会方案")
//    @PreAuthorize("@ss.hasPermission('biz:meeting-plan:create')")
    public CommonResult<Integer> createMeetingPlan(@Valid @RequestBody MeetingPlanSaveReqVO createReqVO) {
        return success(meetingPlanService.createMeetingPlan(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新参会方案")
    //   @PreAuthorize("@ss.hasPermission('biz:meeting-plan:update')")
    public CommonResult<Boolean> updateMeetingPlan(@Valid @RequestBody MeetingPlanSaveReqVO updateReqVO) {
        meetingPlanService.updateMeetingPlan(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除参会方案")
    @Parameter(name = "id", description = "编号", required = true)
    //  @PreAuthorize("@ss.hasPermission('biz:meeting-plan:delete')")
    public CommonResult<Boolean> deleteMeetingPlan(@RequestParam("id") Integer id) {
        meetingPlanService.deleteMeetingPlan(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除参会方案")
    //  @PreAuthorize("@ss.hasPermission('biz:meeting-plan:delete')")
    public CommonResult<Boolean> deleteMeetingPlanList(@RequestParam("ids") List<Integer> ids) {
        meetingPlanService.deleteMeetingPlanListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得参会方案")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('biz:meeting-plan:query')")
    public CommonResult<MeetingPlanRespVO> getMeetingPlan(@RequestParam("id") Integer id) {
        MeetingPlanDO meetingPlan = meetingPlanService.getMeetingPlan(id);
        return success(BeanUtils.toBean(meetingPlan, MeetingPlanRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得参会方案分页")
    //@PreAuthorize("@ss.hasPermission('biz:meeting-plan:query')")
    public CommonResult<PageResult<MeetingPlanRespVO>> getMeetingPlanPage(@Valid MeetingPlanPageReqVO pageReqVO) {
        PageResult<MeetingPlanDO> pageResult = meetingPlanService.getMeetingPlanPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MeetingPlanRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出参会方案 Excel")
    //@PreAuthorize("@ss.hasPermission('biz:meeting-plan:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMeetingPlanExcel(@Valid MeetingPlanPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MeetingPlanDO> list = meetingPlanService.getMeetingPlanPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "参会方案.xls", "数据", MeetingPlanRespVO.class,
                        BeanUtils.toBean(list, MeetingPlanRespVO.class));
    }

}